package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
)

type FankuilistRBody struct {
	Data       []models.Fankui `json:"data"`
	PageNo     int             `json:"pageNo"`
	PageSize   int             `json:"pageSize"`
	TotalCount int64           `json:"totalCount"`
	TotalPage  int64           `json:"totalPage"`
}

type FankuilistBody struct {
	Page     int    `json:"pageNo" from:"pageNo"`
	Pagesize int    `json:"pageSize" from:"pageSize"`
	Phone    string `json:"phone" from:"phone"`
	Status   int64  `gorm:"column:status" json:"status"`
	Inttime  int64  `gorm:"column:inttime" json:"inttime"`
	Endtime  int64  `gorm:"column:endtime" json:"endtime"`
}

func Fankuilist(ctx *gin.Context) {
	request := FankuilistBody{}
	ctx.ShouldBind(&request)
	phone := request.Phone
	inttime := request.Inttime
	endtime := request.Endtime
	status := request.Status
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Fankui
	res := data_source.Db
	if phone != "" {
		res = res.Where("phone = ?", phone)
	}
	if status > 0 {
		res = res.Where("status = ?", status)
	}
	if inttime > 0 {
		res = res.Where("addtime >= ?", inttime)
	}
	if endtime > 0 {
		endcreatetime := endtime + 86400
		res = res.Where("addtime < ?", endcreatetime)
	}
	res.Model(&models.Fankui{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("addtime desc").Find(&users)

	var idarr []int64
	for _, v := range users {
		idarr = append(idarr, v.ID)
	}

	var orders []models.Fankuiordersql
	data_source.Db.Model(&models.Procureorder{}).Select("procureorder.*, fankuiorder.fankuiid, fankuiorder.orderid").Joins("left join fankuiorder on fankuiorder.orderid = procureorder.id").Where("fankuiorder.fankuiid in (?)", idarr).Find(&orders)

	for k, v := range users {
		var os []models.Fankuiordersql
		for _, u := range orders {
			if v.ID == u.Fankuiid {
				os = append(os, u)
			}
		}
		users[k].List = os
	}

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := FankuilistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type FankuishBody struct {
	ID      int64   `json:"id" gorm:"id"`
	Status  int64   `json:"status" gorm:"status"` // 1审核中 2同意 3拒绝 4线索提交中 5线索已发放
	Xsphone string  `json:"xsphone" gorm:"xsphone"`
	Beizhu  string  `json:"beizhu" gorm:"beizhu"`
	Adminid int64   `gorm:"column:adminid" json:"adminid"`
	Price   float64 `json:"price" gorm:"price"`
}

func Fankuish(ctx *gin.Context) {
	request := FankuishBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	status := request.Status
	beizhu := request.Beizhu
	adminid := request.Adminid
	price := request.Price
	fk := models.Fankui{
		Updatetime: time.Now().Unix(),
		Status:     status,
		Beizhu:     beizhu,
		Price:      price,
	}
	data_source.Db.Where("id = ?", id).Updates(&fk)
	if status == 2 {
		var fankui models.Fankui
		data_source.Db.Where("id = ?", id).First(&fankui)

		var user_info models.User
		data_source.Db.Where("id = ?", fankui.Uid).First(&user_info)
		//data := Appaccountuser(user_info.Unionid)
		//fmt.Println(data)
		userprice := user_info.Price + price
		data_source.Db.Model(&models.User{}).Where("id = ?", fankui.Uid).Update("price", userprice)
	}
	if adminid > 0 {
		adminlog := models.Adminlog{
			Adminid: adminid,
			Addtime: time.Now().Unix(),
			Content: "用户提交反馈审核",
		}
		data_source.Db.Create(&adminlog)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

func Fankuixs(ctx *gin.Context) {
	request := FankuishBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	xsphone := request.Xsphone
	adminid := request.Adminid
	fk := models.Fankui{
		Xstime:  time.Now().Unix(),
		Status:  5,
		Xsphone: xsphone,
	}
	data_source.Db.Where("id = ?", id).Updates(&fk)
	if adminid > 0 {
		adminlog := models.Adminlog{
			Adminid: adminid,
			Addtime: time.Now().Unix(),
			Content: "用户提交反馈，发放给用户新的线索",
		}
		data_source.Db.Create(&adminlog)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}
